local skels = {}

local daoshu = fk.CreateSkill{
    name = "ym1__daoshu",
}
Fk:loadTranslationTable{
    ["ym1__daoshu"] = "盗书",
    [":ym1__daoshu"] = "一名角色的结束阶段，若你本回合未使用或被使用过牌，你可视为对牌堆或中央区使用一张【顺手牵羊】。",
    ["Top"] = "牌堆顶",
    ["Bottom"] = "牌堆底",
    ["$CenterArea"] = "中央区",
    ["#ym1__daoshu-invoke"] = "是否发动 盗书，视为对牌堆或中央区使用一张【顺手牵羊】",
    ["#ym1__daoshu_snatch_skill-Pile"] = "顺手牵羊，获得牌堆中一张牌",
    ["#ym1__daoshu_snatch_skill-CenterArea"] = "顺手牵羊：获得中央区内一张牌",

    ["$ym1__daoshu1"] = "得此文书，丞相定可高枕无忧。",
    ["$ym1__daoshu2"] = "让我看看，这是什么机密。",
}
daoshu:addEffect(fk.EventPhaseStart,{
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(daoshu.name) and target.phase == Player.Finish then
            return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                local use = e.data
                return use.from == player or table.contains(use.tos,player)
            end, Player.HistoryTurn) == 0
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices = {}
        if #room.draw_pile > 0 then
            table.insert(choices,"pile_draw")
        end
        if #(room:getBanner("@$CenterArea") or {}) > 0 then
            table.insert(choices,"$CenterArea")
        end
        if #choices > 0 then
            table.insert(choices,"Cancel")
            local choice = room:askToChoice(player,{
                choices = choices,
                skill_name = daoshu.name,
                prompt = "#ym1__daoshu-invoke",
            })
            if choice ~= "Cancel" then
                event:setCostData(self,choice)
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local card = Fk:cloneCard("snatch")
        card.skillName = daoshu.name
        room:useCard{
            from = player,
            tos = {player},
            card = card,
            extra_data = {
                ym1__daoshu = event:getCostData(self)
            },
            noIndicate = true,
        }
    end,
})
daoshu:addEffect(fk.PreCardEffect,{
    can_refresh = function (self, event, target, player, data)
        return target == player and data and data.card.trueName == "snatch" and data.card.skillName == daoshu.name
    end,
    on_refresh = function(self, event, target, player, data)
        local card = data.card:clone()
        local c = table.simpleClone(data.card)
        for k, v in pairs(c) do
            card[k] = v
        end
        card.skill = Fk.skills["ym1__daoshu_snatch_skill"]
        data.card = card
    end,
})
daoshu:addAcquireEffect(function (self, player, is_start)
    local room = player.room
    room:addSkill("#CenterArea")
end)
table.insert(skels,daoshu)

local snatch_skill = fk.CreateSkill{
    name = "ym1__daoshu_snatch_skill"
}
snatch_skill:addEffect("cardskill",{
    mute = true,
    distance_limit = 1,
    mod_target_filter = function(self, player, to_select, selected, card, extra_data)
        return to_select ~= player and
        not (to_select:isAllNude() or
            (not (extra_data and extra_data.bypass_distances) and not self:withinDistanceLimit(player, false, card, to_select)))
    end,
    target_filter = Util.CardTargetFilter,
    target_num = 1,
    on_effect = function(self, room, effect)
        if effect.from.dead then return end
        local player = effect.from
        local cid = {}
        local y = math.max(1,math.min(math.floor((#room.draw_pile-1)/2),5))
        if effect.extra_data.ym1__daoshu == "$CenterArea" and #(room:getBanner("@$CenterArea") or {}) > 0 then
            local card = room:askToChooseCard(effect.from, {
                target = effect.from,
                flag = { card_data = {{ "$CenterArea", room:getBanner("@$CenterArea") }} },
                skill_name = "snatch_skill",
                prompt = "#ym1__daoshu_snatch_skill-CenterArea",
            })
            table.insert(cid,card)
        else
            local card_data, top_cards, center_cards, bottom_cards, visible_data = {}, {}, {}, {}, {}
            if #room.draw_pile > 0 and #room.draw_pile < 6 then
                for i = 1, #room.draw_pile, 1 do
                    table.insert(center_cards,-i-1000)
                    visible_data[tostring(-i-1000)] = false
                end
            else
                if #room.draw_pile > 2 then
                    center_cards = {-1006}
                    visible_data[tostring(-1006)] = false
                end
                for i = 1, y, 1 do
                    table.insert(top_cards,-i-1000)
                    visible_data[tostring(-i-1000)] = false
                    if #room.draw_pile > 1 then
                        table.insert(bottom_cards,-7+i-y-1000)
                        visible_data[tostring(-7+i-y-1000)] = false
                    end
                end
            end
            if #top_cards > 0 then table.insert(card_data,{ "Top", top_cards }) end
            if #center_cards > 0 then table.insert(card_data,{ "pile_draw", center_cards }) end
            if #bottom_cards > 0 then table.insert(card_data,{ "Bottom", bottom_cards }) end
            local card = room:askToChooseCard(effect.from, {
                target = effect.from,
                flag = { card_data = card_data, visible_data = visible_data },
                skill_name = "snatch_skill",
                prompt = "#ym1__daoshu_snatch_skill-Pile",
            })
            --[[local req = Request:new(player, "AskForCardChosen")
            req:setData(player, {player.id, { card_data = card_data, visible_data = visible_data }, "snatch_skill", "#ym1__daoshu_snatch_skill-Pile"})
            local card = req:getResult(player)]]--
            if card+1000 > -6 then
                local n = -card-1000
                table.insert(cid,room.draw_pile[n])
            elseif card+1000 < -6 then
                local n = #room.draw_pile+card+1000+7
                table.insert(cid,room.draw_pile[n])
            else
                local n = #room.draw_pile - y > 6 and math.random(6,#room.draw_pile-y) or math.random(#room.draw_pile)
                table.insert(cid,room.draw_pile[n])
            end
        end
        room:obtainCard(effect.from, cid, false, fk.ReasonPrey, effect.from, "snatch_skill")
    end,
})
table.insert(skels,snatch_skill)

return skels